package com.alaeat.entity;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.text.DecimalFormat;
import java.text.ParseException;

/**
 * @author cuishidong
 */
@Data
@NoArgsConstructor
public class ReportData {
    private String dimension;
    private String activeUsers;
    private String sessions;
    private String engagedSessions;
    private String averageSessionDuration;
    private String sessionsPerUser;
    private String eventsPerSession;
    private String engagementRate;
    private String eventCount;
    private String conversions;
    private String totalRevenue;

    public ReportData(String dimension, String activeUsers, String sessions, String engagedSessions, String averageSessionDuration, String sessionsPerUser, String eventsPerSession, String engagementRate, String eventCount, String conversions, String totalRevenue) {
        this.dimension = dimension;
        this.activeUsers = formatNumber(activeUsers);
        this.sessions = formatNumber(sessions);
        this.engagedSessions = formatNumber(engagedSessions);
        this.averageSessionDuration = averageSessionDuration;
        this.sessionsPerUser = sessionsPerUser;
        this.eventsPerSession = formatNumberPoint(eventsPerSession);
        this.engagementRate = formatPercentage(engagementRate);
        this.eventCount = formatNumber(eventCount);
        this.conversions = formatNumberPoint(conversions);
        this.totalRevenue = formatNumberPoint(totalRevenue);
    }
    public static String formatNumber(String input) {
        DecimalFormat formatter = new DecimalFormat("#,###");
        try {
            Number number = formatter.parse(input);
            return formatter.format(number);
        } catch (ParseException e) {
            // 如果解析失败，返回原数据
            return input;
        }
    }

    public static String formatNumberPoint(String input) {
        DecimalFormat formatter = new DecimalFormat("#,###.00");
        double number = Double.parseDouble(input);
        return formatter.format(number);
    }

    public static void main(String[] args) {
        System.out.println(formatPercentage("0.78400304221067307"));
    }
    public static String formatPercentage(String input) {
        DecimalFormat formatter = new DecimalFormat("0.00%");
        double number = Double.parseDouble(input);
        return formatter.format(number);
    }
}
